package com.hotel.BL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.Util;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.HouseKeeping;

public class HouseKeepingLogic extends AbstractBL {
	
	public HouseKeepingLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	
	public HouseKeeping UpdateCurrentHouseKeeping(long hotelId,long roomId,String reference,String userId) throws EntityNotFoundException
	{
		GenericLogic gLogic = new GenericLogic(req,resp);
		Date d = gLogic.ServerDateFromNightAudit(hotelId);
		HouseKeeping hk = GetHouseKeeping(hotelId,roomId,d);
		if(hk==null){
			hk = new HouseKeeping();
			hk.setReference("");
			hk.setStaffId(-1);
		}
		hk.setHotelId(hotelId);
		hk.setRoomId(roomId);
		hk.setStatusdate(d);
		if(reference!=null)
			hk.setReference(reference);
		if(userId!=null){
			hk.setStaffId(Long.parseLong(userId));
		}
		Key k = datastore.put(hk.getEntity());
		hk.setId(k.getId());
		hk.setKey(k);
		return hk;
	}
	
	public List<HouseKeeping> GetCurrentHouseKeepingByHotelId(long hotelId){
		GenericLogic gLogic = new GenericLogic(req,resp);
		
		Date today = Util.GetOnlyDate(gLogic.ServerDateFromNightAudit(hotelId));
		
		Query q = new Query(EntityName.HouseKeeping);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterDate = new FilterPredicate("statusdate",
				FilterOperator.EQUAL, Util.GetOnlyDate(today));
		Filter filter = CompositeFilterOperator.and(filterHotel, filterDate);

		q.setFilter(filter).addSort("roomId");
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public HouseKeeping GetHouseKeeping(long hotelId,long roomId,Date d) {
		Query q = new Query(EntityName.HouseKeeping);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterRoom = new FilterPredicate("roomId",
				FilterOperator.EQUAL, roomId);
		Filter filterDate = new FilterPredicate("statusdate",
				FilterOperator.EQUAL, Util.GetOnlyDate(d));

		Filter filter = CompositeFilterOperator.and(filterHotel, filterRoom, filterDate);

		q.setFilter(filter);

		return GetHouseKeeping(datastore.prepare(q).asSingleEntity());
	}
	
	private HouseKeeping GetHouseKeeping(Entity entity) {
		if(entity == null) return null;
		HouseKeeping item = new HouseKeeping();
		item.setEntity(entity);
		return item;
	}

	private List<HouseKeeping> GetList(List<Entity> entities) {
		List<HouseKeeping> list = new ArrayList<HouseKeeping>();
		if(entities!=null)
		for (Entity e : entities) {
			HouseKeeping a = new HouseKeeping();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
